<template>
  <div class="container">
    <div class="search-box">
      <el-input
        placeholder="请输入内容"
        class="input-box"
        v-model="textForm.name"
        type="text"
      ></el-input>
      <el-button @click="clearText">清除</el-button>
      <el-button type="primary" @click="searchTask">搜索</el-button>
    </div>
    <el-table border style="width: 100%" :data="list">
      <el-table-column type="index" label="任务编号" width="80"> </el-table-column>
      <el-table-column prop="name" label="任务名称"> </el-table-column>
      <el-table-column prop="des" label="任务描述"> </el-table-column>
      <el-table-column fixed="right" label="操作" width="180">
        <template #default="{ row }">
          <el-button size="mini" style="margin-right: 10px" @click="editTask(row.id)"
            >编辑</el-button
          >
          <el-popconfirm
            confirmButtonText="好的"
            cancelButtonText="不用了"
            icon="el-icon-info"
            iconColor="red"
            title="确定删除任务吗？"
            @confirm="delTask(row.id)"
          >
            <template #reference>
              <el-button size="mini" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="pageContainer">
      <el-pagination
        layout="prev, pager, next"
        :total="total"
        :current-page="params._page"
        :page-size="params._limit"
        @current-change="pageChange"
      >
      </el-pagination>
      <el-button size="small" plain @click="addDialog">+ 新增</el-button>
    </div>
    <!-- 新增弹框 -->
    <el-dialog title="提示" :visible="dialogVisible" width="30%" @close="closeDialog">
      <template #default>
        <el-form ref="textForm" :model="textForm" :rules="rules" label-width="100px">
          <el-form-item label="任务名称" prop="name">
            <el-input v-model="textForm.name" />
          </el-form-item>
          <el-form-item label="任务描述" prop="des">
            <el-input v-model="textForm.des" />
          </el-form-item>
        </el-form>
      </template>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
          <el-button type="primary" @click="confirm">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {
  fetchgetList,
  fetchAddTask,
  fetchEditTask,
  fetchDetailTask,
  fetchDelTask,
  fetchSearchTask
} from '@/api/index.js'
export default {
  data() {
    return {
      list: [],
      textForm: {
        name: '',
        des: '',
        id: ''
      },
      rules: {
        name: [{ required: true, message: '任务名称必填', trigger: 'blur' }],
        des: [{ required: true, message: '任务描述必填', trigger: 'blur' }]
      },
      params: {
        _page: 1, // 当前第几页
        _limit: 5 // 每页条数
      },
      total: 0,
      dialogVisible: false
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    //获取列表
    async getList() {
      const res = await fetchgetList(this.params)
      // console.log(res)
      this.list = res.data
      this.total = Number(res.headers['x-total-count'])
    },
    //改变页数渲染
    pageChange(page) {
      this.params._page = page
      this.getList()
    },
    //新增
    addDialog() {
      this.dialogVisible = true
    },
    //关闭弹框
    closeDialog() {
      this.dialogVisible = false
    },
    cancel() {
      this.resetForm()
    },
    //重置表单
    resetForm() {
      this.closeDialog()
      this.$refs.textForm.resetFields()
      this.textForm = {
        name: '',
        des: ''
      }
    },
    confirm() {
      this.$refs.textForm.validate(async valid => {
        if (valid) {
          if (this.textForm.id) {
            //调用编辑
            await fetchEditTask(this.textForm)
          } else {
            //调用新增
            await fetchAddTask(this.textForm)
          }
          // 提示用户
          this.$message.success(`${this.textForm.id ? '编辑' : '新增'}成功`)
          //重置表单 关闭弹框
          this.getList()
          this.resetForm()
        }
      })
    },
    //编辑
    async editTask(id) {
      this.dialogVisible = true
      const res = await fetchDetailTask(id)
      console.log(res)
      this.textForm = res.data
    },
    //删除
    async delTask(id) {
      await fetchDelTask(id)
      this.$message.success('删除成功')
      this.getList()
    },
    //搜索
    async searchTask() {
      const res = await fetchSearchTask(this.textForm.name)
      console.log(res)
      this.list = res.data
    },
    //清除
    clearText() {
      this.textForm.name = ''
      this.getList()
    }
  }
}
</script>

<style scoped lang="less">
.container {
  width: 800px;
  margin: 0 auto;
}
.search-box {
  display: flex;
  margin-bottom: 10px;
  .input-box {
    margin-right: 20px;
  }
}
.pageContainer {
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
}
.dialog-footer {
  margin-top: 20px;
}
</style>
